package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// Rule holds the schema definition for the Rule entity.
type Rule struct {
	ent.Schema
}

// Fields of the Rule.
func (Rule) Fields() []ent.Field {
	return []ent.Field{
		field.String("nickname"),

		field.Float("max_alarm").Nillable().Optional(),
		field.Float("min_alarm").Nillable().Optional(),
		field.Int("alarm_level").Range(1, 3).Comment("告警等级, 1: warning, 2: panic, 3: fatal"),
		field.Bool("enable").Default(true),
	}
}

// Edges of the Rule.
func (Rule) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("to_object", Object.Type).Unique(),
		edge.To("in_metric", Metric.Type).Unique().Required(),
	}
}

func (Rule) Indexes() []ent.Index {
	return []ent.Index{
		index.Edges("to_object", "in_metric").Fields("alarm_level").Unique(),
	}
}
